/**
 * Created by revathyr on 7/2/14.
 */




import java.util.*;
import java.util.Map.Entry;
class Main{
    public static HashMap<String,Integer> lhm;
    public static  HashMap<Integer,Character > numToRoman;
    public  HashMap<Character,Integer> sToi;
    public static HashMap<String,Integer> wordToValueMap;
    //public static  char[] ip ;
    private  String I="1";
    private String V="5";
    private String X="10";
    private  String L="50";
    private  String C="100";
    private String D="500";
    private  String M="1000";
    public String input;
    public String metal;
    public float silver=1;
    public float gold=1;
    public String word;
    public float iron=1;
    public String modifiedInput;

     Main(){
        silver=0;
        gold=0;
        iron=0;
        numToRoman = new HashMap<Integer,Character>();
        numToRoman.put(1,'I');
        numToRoman.put(5,'V');
        numToRoman.put(10,'X');
        numToRoman.put(50,'L');
        numToRoman.put(100,'C');
        numToRoman.put(500,'D');
        numToRoman.put(1000,'M');
         lhm=new HashMap<String,Integer>();
         lhm.put("I",1);
         lhm.put("V",5);
         lhm.put("L",50);
         lhm.put("C",100);
         lhm.put("D",500);
         lhm.put("M",1000);
         modifiedInput="";
         sToi=new HashMap<Character,Integer>();
         sToi.put('I', 1);
         sToi.put('V',5);
         sToi.put('X',10);
         sToi.put('L',50);
         sToi.put('C', 100);
         sToi.put('D',500);
         sToi.put('M',1000);

    }
    public void get(){
        Scanner in = new Scanner(System.in);
        //System.out.println("enter string and number");
        wordToValueMap =new HashMap<String,Integer>();
        int n = 1;
        while (n != 0) {

            System.out.println("enter string and number");
            String s = in.next();
            int s1 = in.nextInt();
            wordToValueMap.put(s, s1);

            System.out.println(s1);

            System.out.println("press 0 to stop");
            n = in.nextInt();

        }

    }
    public float getInput()
    {   Scanner in =new Scanner(System.in);
        System.out.println("enter credentials");
        input= in.nextLine();
        System.out.println("input u entered is"+ input);
        String convertedInput=wordToRoman(input);
        float noOfUnits=compute(convertedInput);
        float oneUnitOfMetal=priceOfMetal(noOfUnits);
        return oneUnitOfMetal;

    }
    public float getInputProblem()
    {
        Scanner in =new Scanner(System.in);
        float result=0;
        System.out.println("enter the quantity u want");
        word=in.nextLine();
        String convertedInput=wordToRoman(word);
        float noOfUnits=compute(convertedInput);
        String[] splitup= word.split(" ");
        int i= splitup.length;
        metal=splitup[i-3];
        if (metal.equals("silver"))
        {
            result=totalValue(silver,noOfUnits);
        }

        else if (metal.equals( "gold"))

             result=totalValue(gold,noOfUnits);
        else if (metal.equals("iron"))
            result=totalValue(iron,noOfUnits);

        return result;


    }
    public float totalValue(float metal,float noOfUnits)
    {

        return metal*noOfUnits;

    }

   public String wordToRoman(String input)
    {
        //this.input=input;

        int n=0;
        int value=0;
        float  totalPrice=0;
        String w="";

        String[] splitup= input.split(" ");
        int i= splitup.length;
        System.out.println("value of length string is" + i);
        metal=splitup[i-3];


        int temporary = 0;
        while (temporary < (i - 3)) {
            int temp1 = wordToValueMap.get(splitup[temporary]);
            w = w + numToRoman.get(temp1);
            temporary++;
            System.out.println("the word is" + w);
        }


        n = this.checkString(w);
        System.out.println("the modified string in wordToRomanConvert  " + modifiedInput);
        System.out.println("result from checkstring  "+ n);

        if (n == 1) {

            int j = 0;
            totalPrice = compute(modifiedInput);
            System.out.println("the total price computed is  " + totalPrice);


        }
        return modifiedInput;






    }
    public  int isGreater(char c1,char c2){

        int n1=0,n2=0;

        n1=sToi.get(c1);
        n2=sToi.get(c2);
        if(n1>n2)
            return 1;
        else
            return 0;



    }



    public int checkString(String word)
    {
        int i=0;
        int count=1;
        int temp=i+1;
        modifiedInput="";

        if (word.length() == 1) {
            modifiedInput=word;
            return 1;
        }
        while(i<(word.length())) {


            if (i <(word.length() - 1)) {
                temp = i + 1;
                System.out.println("i is" + i);
                if (word.charAt(temp) == word.charAt(i)) {
                    count = count + 1;
                    modifiedInput = modifiedInput + word.charAt(i);
                    i++;

                    if (count > 3) {
                        System.out.print("invalid input");
                        return -1;
                    }
                } else {
                    count=0;
                    int j = isGreater(word.charAt(i), word.charAt(temp));
                    if (j == 0) {
                        if (word.charAt(i) == 'I') {

                            if (!(word.charAt(temp) == 'V' || word.charAt(temp) == 'X')) {
                                System.out.println("invalid input");
                                return -1;
                            } else {

                                modifiedInput = modifiedInput + String.format("-%s", word.charAt(i));
                            }
                        } else {
                            if (word.charAt(i) == 'X') {
                                if (!(word.charAt(temp) == 'L' || word.charAt(temp) == 'C')) {
                                    System.out.println("invalid input");
                                    return -1;
                                } else {

                                    modifiedInput = modifiedInput + String.format("-%s", word.charAt(i));
                                }

                            } else {
                                if (word.charAt(i) == 'C') {
                                    if (!((word.charAt(temp) == 'D') || (word.charAt(temp) == 'M'))) {
                                        System.out.println("invalid input");
                                        return -1;
                                    } else {

                                        modifiedInput = modifiedInput + String.format("-%s", word.charAt(i));
                                    }
                                } else {
                                    System.out.println("invalid input");
                                    return -1;
                                }
                            }
                        }
                    } else {
                        modifiedInput += word.charAt(i);

                    }
                    ++i;
                }
               // i++;
            }
            else
            {   modifiedInput= modifiedInput+word.charAt(i);
            i++;}
        }

        return 1;

    }

    public float priceOfMetal(float totalPrice)

    {

        String[] splitup= input.split(" ");
        int i= splitup.length;
        int value=0;
        metal=splitup[i-3];
        String temp=splitup[i-2];

        value= Integer.parseInt(temp);
        if (metal.equals("silver"))
        {  silver = (value / totalPrice);
            return silver;
        }

        else if (metal.equals( "gold")) {
            gold = (value / totalPrice);
            return gold;
        }
        else if (metal.equals("iron")) {
            iron = (value / totalPrice);
            return iron;
        }
        else
            System.out.println("no such metal");

        return 0;
    }





    public float compute(String input)
    {
        int i=0;
        float total=0;

        //  ++i;
        while (i < input.length()) {

            if (input.length() == 1) {

                total = sToi.get(input.charAt(i));
System.out.println("inside compute.."+ total);
                return total;

            } else if (input.charAt(i) == '-')

            {
                total = total + (sToi.get(input.charAt(i + 2)) - sToi.get(input.charAt(i + 1)));
                i = i + 3;
            } else {

                  total=total+sToi.get(input.charAt(i));



                i++;


            }
        }
        return total;
    }




}

public  class merchantGuide{

    public static void main(String args[]) {

        Main ob=new Main();
        ob.get();

        float r=ob.getInput();
        System.out.println("the val returned is"+ r);
        float res=ob.getInputProblem();
        System.out.println("the val returned is"+ res);








    }}




